Advanced Functions Test

For this test, you should use the built-in functions to be able to write the requested functions in one line.

Problem 1

Use map to create a function which finds the length of each word in the phrase (broken by spaces) and return the values in a list.

The function will have an input of a string, and output a list of integers.


In [5]:
def word_lengths(phrase):
    
    # return map(lambda word: len(word), [word for word in phrase.split()])
    return map(lambda word: len(word), phrase.split())

In [6]:
word_lengths('How long are the words in this phrase')


Out[6]:
[3, 4, 3, 3, 5, 2, 4, 6]

Problem 2

Use reduce to take a list of digits and return the number that they correspond to. Do not convert the integers to strings!


In [11]:
def digits_to_num(digits):
    
    return reduce(lambda x,y: 10*x+y, digits)

In [12]:
digits_to_num([3,4,3,2,1])


Out[12]:
34321

Problem 3

Use filter to return the words from a list of words which start with a target letter.


In [20]:
def filter_words(word_list, letter):
    
    # first attempt
    #return filter(lambda x: x == letter, [word[0] for word in word_list])
    
    return filter(lambda x: x[0] == letter, [word for word in word_list])
    # not this is reduntant. the following will just do
    # return filter(lambda word: word[0]==letter,word_list)

In [21]:
l = ['hello','are','cat','dog','ham','hi','go','to','heart']
filter_words(l,'h')


Out[21]:
['hello', 'ham', 'hi', 'heart']

Problem 4

Use zip and list comprehension to return a list of the same length where each value is the two strings from L1 and L2 concatenated together with connector between them. Look at the example output below:


In [22]:
def concatenate(L1, L2, connector):
    
    return [x+connector+y for x,y in zip(L1,L2)]

In [23]:
concatenate(['A','B'],['a','b'],'-')


Out[23]:
['A-a', 'B-b']

Problem 5

Use enumerate and other skills to return a dictionary which has the values of the list as keys and the index as the value. You may assume that a value will only appear once in the given list.


In [30]:
def d_list(L):
    dout = {}
    for item,val in enumerate(L):
        dout[val] = item
    return dout

    # first attempt return list
    #return [{val:item} for item,val in enumerate(L)]
    
    # or simply
    # return {key:value for value,key in enumerate(L)}
    # use of dictionary comprehension is surprising

In [31]:
d_list(['a','b','c'])


Out[31]:
{'a': 0, 'b': 1, 'c': 2}

Problem 6

Use enumerate and other skills from above to return the count of the number of items in the list whose value equals its index.


In [59]:
def count_match_index(L): 
    return len(filter(lambda x: x[0]==x[1], [pair for pair in enumerate(L)]))
    
    # attempts
    # return [pair for pair in enumerate(L)]
    #return filter(lambda[pair for pair in enumerate(L)])

    # portilla's answer
    # return len([num for count,num in enumerate(L) if num == count])

In [60]:
count_match_index([0,2,2,1,5,5,6,10])


Out[60]:
4

Great Job!